package com.hj.ct.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.hj.ct.db.DBConstants;
import com.hj.ct.parse.GatherData;

public class GatherDataDao extends SQLBaseDao {

	public GatherDataDao(Context context) {
		super(context);
	}

	/**
	 * 插入数据
	 * 
	 * @param message
	 */
	public void insertGatherData(List<GatherData> datas, boolean isRealData) {
		db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		for (GatherData data : datas) {
			ContentValues values = getCV(data, isRealData);
			db.insert(DBConstants.SQL_GATHER_TABLE, null, values);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	public ContentValues getCV(GatherData data, boolean isRealData) {
		ContentValues values = new ContentValues();
		values.put("recTime", data.getRecTime());
		values.put("deviceID", data.getDeviceID());
		values.put("LAFRealTime", data.getLAFRealTime());
		values.put("LAFMaxPrf", data.getLAFMaxPrf());
		values.put("LAeqPrf", data.getLAeqPrf());
		values.put("LCpeakPrf", data.getLCpeakPrf());
		values.put("LAMaxPer", data.getLAMaxPer());
		values.put("LAwqPer", data.getLAwqPer());
		values.put("LAF10Per", data.getLAF10Per());
		values.put("LAF90Per", data.getLAF90Per());
		values.put("LAF95Per", data.getLAF95Per());
		values.put("dustInst", data.getDustInst());
		values.put("Latitude", data.getLatitude());
		values.put("Longitude", data.getLongitude());
		values.put("windSP", data.getWindSP());
		values.put("windDir", data.getWindDir());
		values.put("tempreture", data.getTempreture());
		values.put("humidity", data.getHumidity());
		values.put("pressure", data.getPressure());
		values.put("isReal", isRealData ? 1 : 0);
		return values;
	}

	/**
	 * 删除数据
	 * 
	 * @param message
	 */
	public void deleteGatherData(boolean isRealData) {
		db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		db.delete(DBConstants.SQL_GATHER_TABLE, "isReal=" + (isRealData ? 1 : 0), null);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	public List<GatherData> getGatherData(boolean isRealData) {
		db = dbHelper.getReadableDatabase();
		List<GatherData> datas = new ArrayList<GatherData>();
		Cursor cursor = db.query(DBConstants.SQL_GATHER_TABLE, null, "isReal=" + (isRealData ? 1 : 0), null, null, null, "id asc");
		while (cursor.moveToNext()) {
			GatherData data = new GatherData();
			data.setRecTime(cursor.getLong(cursor.getColumnIndex("recTime")));
			data.setDeviceID(cursor.getString(cursor.getColumnIndex("deviceID")));
			data.setLAFRealTime(cursor.getString(cursor.getColumnIndex("LAFRealTime")));
			data.setLAFMaxPrf(cursor.getString(cursor.getColumnIndex("LAFMaxPrf")));
			data.setLAeqPrf(cursor.getString(cursor.getColumnIndex("LAeqPrf")));
			data.setLCpeakPrf(cursor.getString(cursor.getColumnIndex("LCpeakPrf")));
			data.setLAMaxPer(cursor.getString(cursor.getColumnIndex("LAMaxPer")));
			data.setLAwqPer(cursor.getString(cursor.getColumnIndex("LAwqPer")));
			data.setLAF10Per(cursor.getString(cursor.getColumnIndex("LAF10Per")));
			data.setLAF90Per(cursor.getString(cursor.getColumnIndex("LAF90Per")));
			data.setLAF95Per(cursor.getString(cursor.getColumnIndex("LAF95Per")));
			data.setDustInst(cursor.getString(cursor.getColumnIndex("dustInst")));
			data.setLatitude(cursor.getDouble(cursor.getColumnIndex("Latitude")));
			data.setLongitude(cursor.getDouble(cursor.getColumnIndex("Longitude")));
			data.setWindSP(cursor.getString(cursor.getColumnIndex("windSP")));
			data.setWindDir(cursor.getString(cursor.getColumnIndex("windDir")));
			data.setTempreture(cursor.getString(cursor.getColumnIndex("tempreture")));
			data.setHumidity(cursor.getString(cursor.getColumnIndex("humidity")));
			data.setPressure(cursor.getString(cursor.getColumnIndex("pressure")));
			datas.add(data);
		}
		db.close();
		return datas;
	}
}
